﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ConnectToDatabase;
using ConnectToDatabase.Model;

namespace QLGiayDep.Forms
{
    /// <summary>
    /// Interaction logic for FormPhieuTraSP.xaml
    /// </summary>
    public partial class FormPhieuTraSP : Page
    {
        private UpdateDB updateDB;
        private DeleteDB deleteDB;
        private SelectDB selectDB;
        private bool m_bSelectData = false;
        private string URL;
        
        public FormPhieuTraSP()
        {
            InitializeComponent();
            dataDSSanPhamLoi.ItemsSource = selectDB.SelectAllPhieuTraSanPhamLoi();
        }

        public FormPhieuTraSP(string _URL)
        {
            InitializeComponent();
            URL = _URL;
            updateDB = (UpdateDB)Activator.GetObject(typeof(UpdateDB), URL + "UPDATEDB_OBJECT");
            deleteDB = (DeleteDB)Activator.GetObject(typeof(DeleteDB), URL + "DELETEDB_OBJECT");
            selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), URL + "SELECTDB_OBJECT");
        }

        private void bntThemThongTin_Click(object sender, RoutedEventArgs e)
        {
            WinThemPhieuTraSanPhamxaml m_winThemPhieuTra = new WinThemPhieuTraSanPhamxaml(URL);
            m_winThemPhieuTra.Show();
        }

        private void dataDSSanPhamLoi_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
             if (!m_bSelectData)
            {
                var item = dataDSSanPhamLoi.SelectedItem as PhieuTra_CTPhieuTra;
                txtMaPT.Text = item.MaPhieuTraHang.ToString();
                txtMaSP.Text = item.MaSanPham.ToString();
                date_NgayTra.Text = item.NgayTra.ToString();
                txtThongTinLoi.Text = item.ThongTinLoiSP.ToString();
            }
            else
            {
                m_bSelectData = false;
                
            }
        }

        private void bntSuaThongTin_Click(object sender, RoutedEventArgs e)
        {
           //update phieu tra
            if (txtMaPT.Text == "")
                MessageBox.Show("Chưa chọn sản phẩm để sửa", "Thông báo");
            else
            {
                DateTime m_dtNgayTra = date_NgayTra.SelectedDate.Value;
                updateDB.UpdatePhieuTraHang(txtMaPT.Text, m_dtNgayTra.ToString("yyyy/MM/dd"));
                updateDB.UpdateCTPhieuTra(txtMaPT.Text, txtMaSP.Text, txtThongTinLoi.Text);
                m_bSelectData = true;
                dataDSSanPhamLoi.ItemsSource = selectDB.SelectAllPhieuTraSanPhamLoi();
            }
        }

        private void bntXoaThongTin_Click(object sender, RoutedEventArgs e)
        {
            if (txtMaPT.Text == "")
                MessageBox.Show("Chưa chọn sản phẩm để xóa", "Thông báo");
            else
            {
                int kt;

                //xoa CT_PhieuTra
                deleteDB.DeleteCTPhieuTra(txtMaPT.Text);
                deleteDB.DeletePhieuTra(txtMaPT.Text);

                txtMaPT.Clear();
                txtMaSP.Clear();
                txtThongTinLoi.Clear();
                m_bSelectData = true;
                dataDSSanPhamLoi.ItemsSource = selectDB.SelectAllPhieuTraSanPhamLoi();
            }
        }

        private void bntCapNhatThongTin_Click(object sender, RoutedEventArgs e)
        {
            dataDSSanPhamLoi.ItemsSource = selectDB.SelectAllPhieuTraSanPhamLoi();
            m_bSelectData = true;
        }

    }
}
